package com.sixth.controller;

import com.sixth.bean.MarketIssue;
import com.sixth.bean.bo.IssueCreateBO;
import com.sixth.bean.bo.IssueListBO;
import com.sixth.bean.bo.IssueBO;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.IssueListVO;
import com.sixth.bean.vo.IssueVO;
import com.sixth.constant.Constant;
import com.sixth.service.AdminIssueService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ljh
 * @version 1.0
 * @describition:
 * @since 2022/9/6 21:44
 **/

@RestController
@RequestMapping("admin/issue")
public class AdminIssueController {

    @Autowired
    AdminIssueService adminIssueService;

    //page=1&limit=20&sort=add_time&order=desc

    @RequiresPermissions(value = {"admin:issue:list"},logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo list(IssueListBO issueListBO){
        IssueListVO issueListVO = adminIssueService.list(issueListBO);

        if(issueListVO.getList() != null){
            return BaseRespVo.ok(issueListVO);
        }
        return BaseRespVo.invalidData();
    }

    //admin/issue/create
    //{question: "可以白嫖吗", answer: "自由美利坚，白嫖每一天"}
    //{"errno":0,"data":{"id":8,"question":"可不可以白嫖",
    // "answer":"可以","addTime":"2022-09-07 10:51:21",
    // "updateTime":"2022-09-07 10:51:21"},"errmsg":"成功"}

    @RequiresPermissions(value = {"admin:issue:create"},logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody IssueCreateBO issueCreateBO){
        MarketIssue marketIssue = adminIssueService.create(issueCreateBO);

        IssueVO issueVO = new IssueVO();
        issueVO.setId(marketIssue.getId());
        issueVO.setAnswer(marketIssue.getAnswer());
        issueVO.setQuestion(marketIssue.getQuestion());
        //只应该更新updateTime
        // issueVO.setAddTime(DateUtils.getFormatDate(marketIssue.getAddTime()));
        // issueVO.setUpdateTime(DateUtils.getFormatDate(marketIssue.getUpdateTime()));
        issueVO.setAddTime(marketIssue.getAddTime());
        issueVO.setUpdateTime(marketIssue.getUpdateTime());

        return BaseRespVo.ok(issueVO);
    }

    //admin/issue/update
    //{"id":7,"question":"","answer":"","addTime":"",
    // "updateTime":"","deleted":false}
    //{"errno":0,"data":{"id":8,"question":"可不可以白嫖",
    // "answer":"不可以","addTime":"2022-09-07 10:51:21",
    // "updateTime":"2022-09-07 11:37:58"},"errmsg":"成功"}

    @RequiresPermissions(value = {"admin:issue:update"},logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody IssueBO issueBO){
        MarketIssue marketIssue = adminIssueService.update(issueBO);

        if(marketIssue == null){
            return BaseRespVo.invalidData();
        }


        IssueVO issueVO = new IssueVO();
        BeanUtils.copyProperties(marketIssue,issueVO);
        // issueVO.setId(marketIssue.getId());
        // issueVO.setAnswer(marketIssue.getAnswer());
        // issueVO.setQuestion(marketIssue.getQuestion());
        //
        // issueVO.setUpdateTime(marketIssue.getUpdateTime());
        // issueVO.setAddTime(marketIssue.getAddTime());
        // issueVO.setDeleted(marketIssue.getDeleted());

        //只应该更新updateTime
        // issueVO.setUpdateTime(DateUtils.getFormatDate(marketIssue.getUpdateTime()));
        // issueVO.setAddTime(DateUtils.getFormatDate(marketIssue.getAddTime()));



        return BaseRespVo.ok(issueVO);
    }

    //delete
    //addTime: "2022-09-07T03:26:07.000+00:00"
    // answer: "自由美利坚，白嫖每一天."
    // deleted: false
    // id: 7
    // question: "自由美利坚，白嫖每一天."
    // updateTime: "2022-09-07T04:02:08.000+00:00"
    @RequiresPermissions(value = {"admin:issue:delete"},logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody IssueBO issueBO){

        int code = adminIssueService.delete(issueBO.getId());
        if(code == Constant.OK){
            return BaseRespVo.ok(null);
        }

        return BaseRespVo.invalidData("删除失败");
    }

}
